<?php

namespace App\Http\Controllers\V1\Admin;

use App\Models\V1\AdvertPlace;
use App\Models\V1\Notice;
use Illuminate\Http\Request;

class AdvertPlaceController extends BaseController
{

    /**
     * 广告位列表
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {

        $query=AdvertPlace::query();
        $pageSize=$request->input('pageSize',10);
        $order=$request->input('order','desc');
        $sort=$request->input('sort','id');

        if($request->filled('title')){
            $query->where('title',$request->input('title',""));
        }

        if($request->filled('code')){
            $query->where('code',$request->input('code',""));
        }

        if($request->filled('begin_at')){
            $query->where('created_at','>=',$request->input('begin_at'));
        }
        if($request->filled('end_at')){
            $query->where('created_at','<=',$request->input('end_at'));
        }
        $query->orderBy($sort,$order);
        $data=$query->paginate($pageSize);
        return ajax_output(200,"成功",$data);
    }


    /**
     * 购买盒子广告位
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $model=new AdvertPlace();
        $model->create([
            'title'=>$request->input('title',""),
            'code'=>$request->input('code',""),
            'remark'=>$request->input('remark',""),
            'state'=>$request->input('state',1)
        ]);

        return ajax_output(200,"成功");
    }

    /**
     * 盒子广告位详情
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $vo=AdvertPlace::find($id);
        return ajax_output(200,"成功",$vo);
    }


    /**
     * 更新盒子广告位
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $vo=AdvertPlace::find($id);
        $vo->update([
            'title'=>$request->input('title',""),
            'code'=>$request->input('code',""),
            'remark'=>$request->input('remark',""),
            'state'=>$request->input('state',1)
        ]);
        return ajax_output(200,"成功");
    }

    /**
     * 删除广告位
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $id = explode(',',$id);
        AdvertPlace::destroy($id);
        return ajax_output(200,"成功");
    }

    /**
     * 广告位列表
     *
     * @return \Illuminate\Http\Response
     */
    public function selectList(Request $request)
    {
        $area_model=new AdvertPlace();
        $data=$area_model->select("id as value",'title as label')->get();

        return ajax_output(200,"成功",$data);
    }
}
